package com.lee.boot.head.first.demo.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Auther: Lee
 * @Date: 2018/6/27 17:03
 * @Description: 定义servlet
 */
//当config使用了@ServletComponentScan注解后，会自动扫描@WebServlet注解
//这是一种自动装配的方式
@WebServlet(urlPatterns = "/test2")
public class HelloServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write("这里是一个servlet的get方法");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
        resp.getWriter().write("这里是一个servlet的post方法");
    }
}
